package com.dongdongshop.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.mapper.TbAddressMapper;
import com.dongdongshop.pojo.user.TbAddress;
import com.dongdongshop.pojo.user.TbAddressExample;
import com.dongdongshop.pojo.vo.AddressVo;
import com.dongdongshop.service.user.AddressService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
@Service
public class AddressServiceImpl implements AddressService {

    @Resource
    private TbAddressMapper addressMapper;

    /*
    * 查询地址管理根据用户名
    * */
    @Override
    public List<AddressVo> getAddressList(String username) {
        List<AddressVo> voAddresses = addressMapper.getAddressListByUserName(username);
        return voAddresses;
    }

    /*
    * 增加
    * */
    @Override
    public void insertAddress(TbAddress address) {
        addressMapper.insertSelective(address);
    }

    /*
    * 删除
    * */
    @Override
    public void deleteAddress(Integer id) {
        addressMapper.deleteByPrimaryKey(id.longValue());
    }

    /*
    * 修改默认地址
    * */
    @Override
    public void updateIsDefault(Integer id) {
        TbAddressExample example = new TbAddressExample();
        TbAddressExample.Criteria criteria = example.createCriteria();
        criteria.andIsDefaultEqualTo("1");
        List<TbAddress> tbAddresses = addressMapper.selectByExample(example);
        //先修改为0
        for (TbAddress tbAddress : tbAddresses) {
            tbAddress.setIsDefault("0");
            addressMapper.updateByPrimaryKeySelective(tbAddress);
        }
        //修改默认
        TbAddress address = addressMapper.selectByPrimaryKey(id.longValue());
        address.setIsDefault("1");
        addressMapper.updateByPrimaryKeySelective(address);
    }

    @Override
    public TbAddress getById(Long addressId) {
        return addressMapper.selectByPrimaryKey(addressId);
    }
}
